/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package updatetree;

import java.io.*;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.*;

import org.dom4j.DocumentException;
import org.dom4j.Element;

import org.encog.neural.data.NeuralDataSet;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation;
import org.encog.persist.EncogPersistedCollection;
import org.encog.util.logging.Logging;

/**
 *
 * @author mani
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, DocumentException, SQLException, ParseException, Exception {
        //  try {

        // TODO code application logic here
        if (CheckAbility()) {
            return;
        }
        Logging.stopConsoleLogging();
        String udir = args[0];
        OpenMysqlConnection omc = new OpenMysqlConnection(udir);
        XMLClass xc = new XMLClass(udir, "savemoney.xml");
        Element rootelement = xc.getRootElement();
        Iterator elementIterator = rootelement.elementIterator();
        while (elementIterator.hasNext()) {
            Element next = (Element) elementIterator.next();

            int updateday = Integer.parseInt(next.attribute("day").getValue());
            S2D update = new S2D(next.attribute("date").getValue());
            boolean isNeedUpdate = update.compareToday(update.getString(), updateday);
            if (!isNeedUpdate) {
                continue;
            }

            String yahoofile = next.attributeValue("yahoo");
            String egfile = next.attributeValue("egfile");
            EncogPersistedCollection epc = new EncogPersistedCollection(udir + "network" + File.separatorChar + egfile);
            BasicNetwork network = (BasicNetwork) epc.find("notebook");
            NeuralDataSet nset = omc.getTodayData(yahoofile);
            if (nset == null) {
                continue;
            }
            ResilientPropagation genetic = new ResilientPropagation(network, nset);

            int count = 0;
            do {
                genetic.iteration();
                count++;
            } while (count < 5000 && genetic.getError() > 0.001);
            epc.delete("notebook");
            epc.add("notebook", network);
            xc.saveUpdateDateToday(next);

            xc.Save();
        }
    }

    private static boolean CheckAbility() throws ParseException {
        update ue = new update("http://hq.sinajs.cn/list=sh000001");

        String sinaline = ue.getsl();
        if (sinaline == null) {
            return true;
        }
        String[] sinadata = sinaline.split(",");

        S2D lastday = new S2D(sinadata[30]);

        S2D today = new S2D(lastday.getTodayChinese());

        if (lastday.getDate().compareTo(today.getDate()) == 0) {
            return false;
        }
        return true;
    }
}
